

using Abp.Authorization.Users;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Yozeev.BusinessLogic.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class TicketRoleManager :YozeevDomainServiceBase, ITicketRoleManager
    {

        private readonly IRepository<TicketRole, Guid> _ticketRoleRepository;
        private readonly IRepository<UserRole, long> _userRoleRepository;

        /// <summary>
        /// TicketRole的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public TicketRoleManager(IRepository<TicketRole, Guid> ticketRoleRepository, IRepository<UserRole, long> userRoleRepository)	{
			_ticketRoleRepository =  ticketRoleRepository;
            _userRoleRepository = userRoleRepository;

        }

		 #region 查询判断的业务

        /// <summary>
        /// 返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns></returns>
        public IQueryable<TicketRole> QueryTicketRoles()
        {
            return _ticketRoleRepository.GetAll();
        }

        /// <summary>
        /// 返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns></returns>
        public IQueryable<TicketRole> QueryTicketRolesAsNoTracking()
        {
            return _ticketRoleRepository.GetAll().AsNoTracking();
        }

        /// <summary>
        /// 根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TicketRole> FindByIdAsync(Guid id)
        {
            var entity = await _ticketRoleRepository.GetAsync(id);
            return entity;
        }

        /// <summary>
        /// 检查实体是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> IsExistAsync(Guid id)
        {
            var result = await _ticketRoleRepository.GetAll().AnyAsync(a => a.Id == id);
            return result;
        }

        #endregion

		 
		 
        public async Task<TicketRole> CreateAsync(TicketRole entity)
        {
            entity.Id = await _ticketRoleRepository.InsertAndGetIdAsync(entity);
            return entity;
        }

        public async Task UpdateAsync(TicketRole entity)
        {
            await _ticketRoleRepository.UpdateAsync(entity);
        }

        public async Task DeleteAsync(Guid id)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _ticketRoleRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDelete(List<Guid> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _ticketRoleRepository.DeleteAsync(a => input.Contains(a.Id));
        }

        //// custom codes

        /// <summary>
        /// 根据角色Id批量删除
        /// </summary>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public async Task BatchDeleteByRole(List<int> roleIds)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _ticketRoleRepository.DeleteAsync(a => roleIds.Contains(a.RoleId));
        }

        public async Task BatchCreate(int roleId,List<Guid> ticketPriceIds)
        {
            foreach(var ticketPriceId in ticketPriceIds) 
            {
                var ticketRole = new TicketRole()
                {
                    RoleId = roleId,
                    TicketPriceId = ticketPriceId,
                };
                await _ticketRoleRepository.InsertAndGetIdAsync(ticketRole);
            }
        }
        public async Task CreateByRole(int roleId, List<Guid> ticketPriceIds)
        {
            if (ticketPriceIds == null ) return;
            var roleList = new List<int>
            {
                roleId
            };
            await BatchDeleteByRole(roleList);
            await BatchCreate(roleId, ticketPriceIds);
        }

        /// <summary>
        /// 根据用户Id查找可销售票价
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public virtual List<Guid> GetTicketPriceIdList(long UserId)
        {
            var userRole = _userRoleRepository.GetAll().FirstOrDefault(m => m.UserId == UserId);
            if (userRole == null) 
                throw new Exception("该用户暂未绑定角色");
            var list = _ticketRoleRepository.GetAll().Where(m => m.RoleId == userRole.RoleId).Select(m=>m.TicketPriceId).ToList();
            return list;
        }

        /// <summary>
        /// 根据用户Id查找可销售票价
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public virtual List<TicketPrice> GetTicketPriceByRole(int RoleId)
        {
            return _ticketRoleRepository.GetAll().Where(m => m.RoleId == RoleId).Select(m => m.TicketPrice).ToList();
        }
        //// custom codes end







    }
}
